﻿Imports System.ComponentModel
Imports DevExpress.XtraBars
Imports DevExpress.XtraBars.Ribbon
Imports DevExpress.XtraBars.Helpers
Imports DevExpress.Skins
Imports DevExpress.LookAndFeel
Imports DevExpress.UserSkins
Imports System.IO
Imports CrystalDecisions.CrystalReports.Engine


Public Class frmMain

    Sub New()
        InitSkins()
        InitializeComponent()
        Me.InitSkinGallery()
    End Sub
    Sub InitSkins()
        DevExpress.Skins.SkinManager.EnableFormSkins()
        DevExpress.UserSkins.BonusSkins.Register()
        UserLookAndFeel.Default.SetSkinStyle("Xmas")
    End Sub
    Private Sub InitSkinGallery()
        SkinHelper.InitSkinGallery(RibbonGalleryBarItem1, True)
    End Sub


    Dim new_control As Control
    Private Sub btnNhanVien_LinkClicked(ByVal sender As System.Object, ByVal e As DevExpress.XtraNavBar.NavBarLinkEventArgs) Handles btnNhanVien.LinkClicked
        PanelQuanLy.Controls.Remove(new_control)
        new_control = New u_NhanVien
        new_control.Dock = DockStyle.Fill
        PanelQuanLy.Controls.Add(new_control)
    End Sub

    Private Sub btnKhachHang_LinkClicked(ByVal sender As System.Object, ByVal e As DevExpress.XtraNavBar.NavBarLinkEventArgs) Handles btnKhachHang.LinkClicked
        PanelQuanLy.Controls.Remove(new_control)
        new_control = New u_KhachHang
        new_control.Dock = DockStyle.Fill
        PanelQuanLy.Controls.Add(new_control)
    End Sub

    Private Sub btnThucDon_LinkClicked(ByVal sender As System.Object, ByVal e As DevExpress.XtraNavBar.NavBarLinkEventArgs) Handles btnThucDon.LinkClicked
        PanelQuanLy.Controls.Remove(new_control)
        new_control = New u_ThucDon
        new_control.Dock = DockStyle.Fill
        PanelQuanLy.Controls.Add(new_control)
    End Sub

    Private Sub btnNguyenLieu_LinkClicked(ByVal sender As System.Object, ByVal e As DevExpress.XtraNavBar.NavBarLinkEventArgs) Handles btnNguyenLieu.LinkClicked
        PanelQuanLy.Controls.Remove(new_control)
        new_control = New u_NguyenLieu
        new_control.Dock = DockStyle.Fill
        PanelQuanLy.Controls.Add(new_control)
    End Sub

    Private Sub iExit_ItemClick(ByVal sender As Object, ByVal e As ItemClickEventArgs) Handles iExit.ItemClick
        Close()
    End Sub

    Private Sub btnLau1_LinkClicked(ByVal sender As Object, ByVal e As DevExpress.XtraNavBar.NavBarLinkEventArgs) Handles btnLau1.LinkClicked
        PanelQuanLy.Controls.Remove(new_control)
        new_control = New u_Ban
        new_control.Dock = DockStyle.Fill
        PanelQuanLy.Controls.Add(new_control)
    End Sub

    Private Sub btnThanhToan_LinkClicked(ByVal sender As Object, ByVal e As DevExpress.XtraNavBar.NavBarLinkEventArgs) Handles btnThanhToan.LinkClicked
        PanelQuanLy.Controls.Remove(new_control)
        new_control = New u_ThanhToanHoaDon
        new_control.Dock = DockStyle.Fill
        PanelQuanLy.Controls.Add(new_control)
    End Sub

    Private Sub DanhSachTKNV_ItemClick(ByVal sender As Object, ByVal e As ItemClickEventArgs) Handles DanhSachTKNV.ItemClick
        PanelQuanLy.Controls.Remove(new_control)
        new_control = New u_taikhoan
        new_control.Dock = DockStyle.Fill
        PanelQuanLy.Controls.Add(new_control)
    End Sub

    Private Sub iFind_ItemClick(ByVal sender As Object, ByVal e As ItemClickEventArgs) Handles iFind.ItemClick
        PanelQuanLy.Controls.Remove(new_control)
        new_control = New u_timkiem
        new_control.Dock = DockStyle.Fill
        PanelQuanLy.Controls.Add(new_control)
    End Sub

    Private Sub btnNhaCungCap_LinkClicked(ByVal sender As Object, ByVal e As DevExpress.XtraNavBar.NavBarLinkEventArgs) Handles btnNhaCungCap.LinkClicked
        PanelQuanLy.Controls.Remove(new_control)
        new_control = New u_NhaCungCap
        new_control.Dock = DockStyle.Fill
        PanelQuanLy.Controls.Add(new_control)
    End Sub

    Private Sub frmMain_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles MyBase.FormClosing
        Application.Exit()
    End Sub
    Dim DSHD1 As DataTable = New DataTable()
    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        PanelQuanLy.Controls.Remove(new_control)
        new_control = New u_GhiChu
        new_control.Dock = DockStyle.Fill
        PanelQuanLy.Controls.Add(new_control)
        If FrmDangNhap.dto.MaNV = 2 Then
            DanhSachTKNV.Enabled = False
            btnPhanQuyen.Enabled = False
            RibbonPageGroup2.Enabled = False
            btnNhanVien.Enabled = False
            dgvHoaDon.Visible = False
        Else : DanhSachTKNV.Enabled = True
            btnPhanQuyen.Enabled = True
            RibbonPageGroup2.Enabled = True
            btnNhanVien.Enabled = True
            dgvHoaDon.Visible = False
        End If
        FrmDangNhap.dto.ID = info.Caption.ToString()


        DSHD1 = HoaDonBUS.Get_DSHD()
        dgvHoaDon.DataSource = DSHD1
        'Dim hd As New HoaDonBUS
        'dgvHoaDon.DataSource = hd.GetHoaDon
    End Sub

    Private Sub BarButtonItem1_ItemClick(sender As Object, e As ItemClickEventArgs) Handles BarButtonItem1.ItemClick
        PanelQuanLy.Controls.Remove(new_control)
        new_control = New u_GhiChu
        new_control.Dock = DockStyle.Fill
        PanelQuanLy.Controls.Add(new_control)
    End Sub

    Private Sub btnNhapNL_LinkClicked(sender As Object, e As DevExpress.XtraNavBar.NavBarLinkEventArgs) Handles btnNhapNL.LinkClicked
        PanelQuanLy.Controls.Remove(new_control)
        new_control = New u_PhieuNhap
        new_control.Dock = DockStyle.Fill
        PanelQuanLy.Controls.Add(new_control)
    End Sub

    Private Sub iBaoCao_ItemClick(sender As Object, e As ItemClickEventArgs) Handles iBaoCao.ItemClick
        PanelQuanLy.Controls.Remove(new_control)
        new_control = New u_Report
        new_control.Dock = DockStyle.Fill
        PanelQuanLy.Controls.Add(new_control)
    End Sub

    Private Sub btnXuatNL_LinkClicked(sender As Object, e As DevExpress.XtraNavBar.NavBarLinkEventArgs) Handles btnXuatNL.LinkClicked
        PanelQuanLy.Controls.Remove(new_control)
        new_control = New u_XuatNguyenLieu
        new_control.Dock = DockStyle.Fill
        PanelQuanLy.Controls.Add(new_control)
    End Sub

    Private Sub btnBackup_ItemClick(sender As Object, e As ItemClickEventArgs) Handles btnBackup.ItemClick

        'Dim strRestoreQuery = String.Format("USE {0} BACKUP DATABASE {0} TO DISK = '{1}' WITH FORMAT, MEDIANAME = '{0}',NAME = 'Full Backup of {0}';", "QLNhaHang.mdb", "D:\")
        'ExcelProvider.NonQuery(strRestoreQuery)
        Try
            Dim f As FileInfo = New FileInfo("QLNhaHang.mdb")
            f.CopyTo("QLNhaHang" + Now.ToString("yyyyMMddhhmm") + ".mdb")
            MessageBox.Show("Backup CSDL thành công !!!")
        Catch t As Exception
            MessageBox.Show("Backup dữ liệu không thành công !! Xin vui lòng thử lại sau 1 phút ")
        End Try
    End Sub

    Private Sub BarButtonItem3_ItemClick(sender As Object, e As ItemClickEventArgs) Handles BarButtonItem3.ItemClick
        Help.ShowHelp(Me, "HuongDan.chm")
    End Sub

    Private Sub BarButtonItem2_ItemClick(sender As Object, e As ItemClickEventArgs) Handles BarButtonItem2.ItemClick
        Dim from As New FrmThongTin()
        from.ShowDialog()
    End Sub

    Private Sub iAbout_ItemClick(sender As Object, e As ItemClickEventArgs) Handles iAbout.ItemClick
        Dim from As New frmDangKy()
        from.ShowDialog()
    End Sub


    Private Sub iKiemKe_ItemClick(sender As Object, e As ItemClickEventArgs) Handles iKiemKe.ItemClick
        MessageBox.Show("Bạn phải tạo ra file .xls trước, ghi rõ tên các cột , trước khi xuất nếu không quá trình xuất sẽ bị lỗi")
        Dim SaveFileDialog1 As SaveFileDialog = New SaveFileDialog()
        SaveFileDialog1.Filter = "Export (*.xls)|*.xls"
        SaveFileDialog1.ShowDialog()
        Dim File_name As String = SaveFileDialog1.FileName

        ExcelProvider.file_name = File_name
        Try

            For i As Integer = 0 To DSHD1.Rows.Count - 1
                Dim sql As String
                sql = String.Format("INSERT INTO [Sheet1$] (MaHD,NgayXuat,MaBan,TenMonAn,MaKH,MaNV,TinhTrang,TongTien) values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}')", DSHD1.Rows(i)(0), DSHD1.Rows(i)(1), DSHD1.Rows(i)(2), DSHD1.Rows(i)(3), DSHD1.Rows(i)(4), DSHD1.Rows(i)(5), DSHD1.Rows(i)(6), DSHD1.Rows(i)(7))
                ExcelProvider.NonQuery(sql)
            Next
            MessageBox.Show("Xuất dữ liệu thành công")
        Catch ex As Exception
            MessageBox.Show("Xuất dữ liệu bị lỗi, bạn vui lòng kiểm tra lại cấu trúc file .xls")
        End Try
    End Sub


End Class
